import React, { useReducer } from "react";

function reducer(state, action) {
  console.log(state, action);
  switch (action.type) {
    case "add":
      return state + 1;
    case "sub":
      return state - 1;
    default:
      throw new Error('异常');
  }
}

function ReducerTest({ initialCount = 0 }) {
  const [count, dispatch] = useReducer(reducer, 0);
  return (
    <>
      your counter is {count}
      <button onClick={() => dispatch({ type: "add" })}>+</button>
      <button onClick={() => dispatch({ type: "sub" })}>-</button>
    </>
  );
}

export default ReducerTest;
